package com.szdvte.news.dao.impl;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.szdvte.news.bean.Users;
import com.szdvte.news.dao.IUserDao;

import org.apache.log4j.Logger;



public class UserDaoImpl extends BaseDaoImpl implements IUserDao 
{
	static Logger logger = Logger.getLogger( UserDaoImpl.class.getName () ) ;

	
	private boolean flag;
	@Override
	public boolean add(Users user) 
	{
		flag = false;
		try {
			this.getSqlMapClient().insert("user.add", user);
			flag = true;
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return flag;
	}

	@Override
	public Users login(Users user) 
	{
		try 
		{
			
			

			user = (Users)this.getSqlMapClient().queryForObject("user.login", user);
			logger.debug("UserLog: " + this.getSqlMapClient());
			//this.getSqlMapClient().
		} 
		catch (SQLException e) 
		{
			e.printStackTrace();
		}
		return user;
	}

	@Override
	public Integer count(Users user) {
		int count = 0;
		try {
			count = (Integer)this.getSqlMapClient().queryForObject("user.count",user);
		} catch (NumberFormatException e) {
			e.printStackTrace();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return count;
	}
	@Override
	public boolean update(Users user) {
		flag = false;
		try {
			getSqlMapClient().update("user.update",user);
			flag = true;
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return flag;
	}

	@Override
	public boolean delete(Users user) {
		flag = false;
		try {
			getSqlMapClient().update("user.delete",user);
			flag = true;
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return flag;
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Users> select() {
		List<Users> users = new ArrayList<Users>();
		try {
			users = getSqlMapClient().queryForList("user.select");
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return users;
	}

	@Override
	public Users load(Users user) {
		try {
			user = (Users) getSqlMapClient().queryForObject("user.load",user);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return user;
	}

}
